package com.example.opencvdemo


import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import org.opencv.android.Utils
import org.opencv.core.Core
import org.opencv.core.Mat
import java.io.IOException


private const val TAG = "XorMatCompose"

@Composable
fun XorMatCompose(modifier: Modifier = Modifier) {
    // 例子创建bitmap，放到image中
//    val bitmap = BitmapFactory.decodeResource(LocalContext.current.resources, R.drawable.pic01)
//    val mat = Mat()
//    Utils.bitmapToMat(bitmap, mat)
//    Image(bitmap = bitmap.asImageBitmap(), contentDescription = null, modifier = modifier)
    val context = LocalContext.current
    Column(
        modifier = modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.SpaceAround
    ) {
        // 测量真实像素的方法
        val opts = BitmapFactory.Options()
        opts.inJustDecodeBounds = true
        BitmapFactory.decodeResource(context.resources, R.drawable.black_red, opts)
        Log.e(TAG, "MainCompose: ${opts.outWidth} ${opts.outHeight}")

        Image(
            bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.black_red)
                .asImageBitmap(), contentDescription = null
        )
        Image(
            bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.black_yellow)
                .asImageBitmap(), contentDescription = null
        )
        val bitmap = xorMat(context, R.drawable.black_red, R.drawable.black_yellow)
        Image(bitmap = bitmap.asImageBitmap(), contentDescription = null)
    }


//    var tvTittle: JavaCameraView? = null
//    AndroidView(factory = {
//        //加载AndroidView布局。
//        LayoutInflater.from(it).inflate(R.layout.image_manipulations_surface_view, null).apply {
//            tvTittle = findViewById(R.id.image_manipulations_activity_surface_view)
//        }
//    }) {
//        //更新UI数据
//
//    }
}

fun xorMat(context: Context, image1: Int, image2: Int): Bitmap {
    val mat1: Mat
    val mat2: Mat
    try {
        mat1 = Utils.loadResource(context, image1)
        mat2 = Utils.loadResource(context, image2)
        Log.e(TAG, "xorMat mat1 mat2: ${mat1.width()} ${mat2.height()}")
    } catch (e: IOException) {
        e.printStackTrace()
        return BitmapFactory.decodeResource(context.resources, image1)
    }
    val dst = Mat()
    // 运算方法
    Core.bitwise_and(mat1, mat2, dst)
    // 转换回bitmap
    Log.e(TAG, "xorMat dst:${dst.width()} ${dst.height()}")
    val newBitmap = Bitmap.createBitmap(dst.width(), dst.height(), Bitmap.Config.ARGB_8888)
    Utils.matToBitmap(dst, newBitmap)
    mat1.release()
    mat2.release()
    dst.release()
    Log.e(TAG, "xorMat: ${newBitmap.width} ${newBitmap.height}")
    return newBitmap
}

// 根据手机的分辨率从 dp 的单位 转成为 px(像素)
fun dip2px(context: Context, dpValue: Float): Int {
    // 获取当前手机的像素密度（1个dp对应几个px）
    val scale = context.resources.displayMetrics.density
    return (dpValue * scale + 0.5f).toInt() // 四舍五入取整
}

// 根据手机的分辨率从 px(像素) 的单位 转成为 dp
fun px2dip(context: Context, pxValue: Float): Int {
    // 获取当前手机的像素密度（1个dp对应几个px）
    val scale = context.resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt() // 四舍五入取整
}